<template>
  <view class="container">
    <!-- 用户状态显示 -->
    <view v-if="username" class="user-info">
		<text class="username">{{ username }}</text>
		<button class="logout-btn" @tap="handleLogout">退出登录</button>
    </view>

    <!-- 登录按钮 (未登录时显示) -->
    <button 
		v-else
		class="login-btn"
		@tap="navigateToLogin"
    >
		登录
    </button>

    <!-- 主功能按钮 -->
	<button 
		class="action-btn"
		@tap="navigateToProduceRepair"
	>
		进仓单管理
	</button>
	<button
		class="action-btn"
		@tap="navigateToPickBill"
	>
		领料单管理
	</button>
	<button
		class="action-btn"
		@tap="navigateToInventoryBill"
	>
		盘点单管理
	</button>
	<button
		class="action-btn"
		@tap="navigateToKittingBill"
	>
		齐套点数
	</button>
	</view>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { onShow } from '@dcloudio/uni-app'



/* 响应式数据 */
const username = ref<string | null>(null);

/* 生命周期钩子 */
onShow(() => {
	loadUserInfo();
});


/* 加载用户信息 */
const loadUserInfo = () => {
	const user = uni.getStorageSync('user');
	if (user) {
		username.value = user.name;
	}
};

/* 跳转登录页面 */
const navigateToLogin = () => {
	uni.navigateTo({ url: '/pages/login/login' });
};

/* 跳转进仓单页面 */
const navigateToProduceRepair = () => {
	console.log(username.value)
	if(!username.value || !username.value.trim()) {
		uni.showToast({
			title: '请先登录！',
			icon: 'none'
		});
		return;
	}
	uni.navigateTo({ url: '/pages/productRepair/productRepair' });
};

/* 跳转领料单页面 */
const navigateToPickBill = () => {	
	if(!username.value || !username.value.trim()) {
		uni.showToast({
			title: '请先登录！',
			icon: 'none'
		});
		return;
	}
	uni.navigateTo({ url: '/pages/wpPickBill/wpPickBill' });
};

/* 跳转齐套点数页面 */
const navigateToKittingBill = () => {
	if(!username.value || !username.value.trim()) {
		uni.showToast({
			title: '请先登录！',
			icon: 'none'
		});
		return;
	}
	uni.navigateTo({ url: '/pages/kitting/kitting' });
};

/* 跳转到盘点单页面 */
const navigateToInventoryBill = () => {
	if(!username.value || !username.value.trim()) {
		uni.showToast({
			title: '请先登录！',
			icon: 'none'
		});
		return;
	}
	uni.navigateTo({
		url: '/pages/InventoryBill/InventoryBill'
	})
}

/* 退出登录 */
const handleLogout = () => {
	//uni.removeStorageSync('user');
	uni.clearStorage();
	username.value = null;
	uni.showToast({ title: '已退出登录', icon: 'success' });
};
</script>

<style scoped>
.container {
  padding: 40rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.user-info {
  width: 100%;
  margin-bottom: 40rpx;
  padding: 20rpx;
  background: #f5f5f5;
  border-radius: 12rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.username {
  font-size: 32rpx;
  color: #333;
}

.login-btn,
.action-btn {
  width: 80%;
  height: 80rpx;
  margin: 30rpx 0;
  background: #007AFF;
  color: white;
  border-radius: 40rpx;
  display: flex;
  justify-content: center;
  align-items: center;
}

.logout-btn {
  padding: 10rpx 20rpx;
  background: #ff3b30;
  color: white;
  border-radius: 20rpx;
}
</style>